package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/5/26.
 */

import com.xianzhiOA.bean.Xzpower;
import com.xianzhiOA.service.XzpowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限表
 *
 * @author 王诗博
 * @create 2017-05-26 15:55
 **/
@Controller
@RequestMapping("/Xzpower")
public class XzpowerController {
    @Autowired
    private XzpowerService powerService;

    //查询所有
    @RequestMapping("/selectAll.emp")
    public ModelAndView selectAll(){
          ModelAndView mv=new ModelAndView("backEnd/powerShow");
        List<Xzpower> powerList=powerService.selAll();
        mv.addObject("powerList",powerList);
          return mv;
    }

    //跳到添加权限页面
    @RequestMapping("/toaddPower.emp")
    public ModelAndView addPower(){
        ModelAndView mv=new ModelAndView("backEnd/powerEdit");
        //查询所有的父级权限
        List<Xzpower> superPowerList=powerService.selsuperNoAll();
        mv.addObject("superPowerList",superPowerList);
        return  mv;
    }

   //添加权限
    @RequestMapping("addPower.emp")
    public ModelAndView addPower(Xzpower xzpower){
       ModelAndView mv=new ModelAndView("redirect:selectAll.emp");
       //模块级为第一级，权限级为第二级

        //添加一个权限先添加它的模板级权限
        Xzpower PowerTypeOfXzpower =powerService.selectByPrimaryKey(xzpower.getPowerId());
        if(PowerTypeOfXzpower == null){

            if(powerService.selAll().size()==0){
                xzpower.setPowerType(1);
            }else {
                int PowerType= powerService.selePowerTypeMaxOne()+1;
                xzpower.setPowerType(PowerType);
            }
        }else {
            xzpower.setPowerType(Integer.parseInt(PowerTypeOfXzpower.getPowerNo()));
        }
       //当无父级权限号时
        if(xzpower.getPowerId() == 0) {
            //数据库中的表没任何数据
            List<Xzpower> superPowerList = powerService.selsuperNoAll();
            if (superPowerList.size() == 0) {
                xzpower.setPowerNo("1");   //添加本级的权限序号为0
                xzpower.setSuperNo("0");   //添加本级的权限序号为1
            } else {
                    String newPowerNo = powerService.selePowerNoMaxOne("0");
                    int newPowerNoInt=Integer.parseInt(newPowerNo)+1;  //新的本级权限号需加1
                    newPowerNo=String.valueOf(newPowerNoInt);
                    xzpower.setPowerNo(newPowerNo);
                    xzpower.setSuperNo("0");
            }
        } else {   //当有父级权限时

                  //根据主键查询
                  if( xzpower.getSuperNo() == null || xzpower.getSuperNo().equals("0")){    //（有模板级没有权限级时）
                      Xzpower newXzpower = powerService.selectByPrimaryKey(xzpower.getPowerId());
                      xzpower.setSuperNo(newXzpower.getPowerNo());
                      //当此父级权限下的子级权限第一个添加时
                      if (powerService.selePowerNoMaxOne(xzpower.getSuperNo()) == null) {
                          xzpower.setPowerNo(String.valueOf(xzpower.getSuperNo()) + "_1");
                      } else {
                          String newPowerNo = powerService.selePowerNoMaxOne(xzpower.getSuperNo());
                          //截取原先的最大的本级权限号后再加上1，得到新的本级权限再加里去
                          String[] units = newPowerNo.split("_");
                          newPowerNo = String.valueOf(Integer.parseInt(units[units.length - 1]) + 1);
                          String superNo = units[0].toString();
                          newPowerNo = superNo + "_" + newPowerNo;   //新的本级权限号
                          xzpower.setPowerNo(newPowerNo);
                      }
                  }else { //（即有模板级有权限级时）
                      String newPowerNo = powerService.selePowerNoMaxOne(xzpower.getSuperNo());
                        if(newPowerNo == null){  //该权限级下第一次添加
                            newPowerNo=xzpower.getSuperNo()+"_1";
                        }else {   //该权限级下不是第一次添加
                            String[] units = newPowerNo.split("_");
                            newPowerNo = String.valueOf(Integer.parseInt(units[units.length - 1]) + 1);
                            newPowerNo=xzpower.getSuperNo()+"_"+newPowerNo;
                        }
                      xzpower.setPowerNo(newPowerNo);
                  }

        }

        //添加权限
       powerService.insertPower(xzpower);
       return mv;
    }


    //跳到查询所有权限
    @RequestMapping("toPowerShowPage.emp")
    public ModelAndView toEmpShowPage(){
      ModelAndView mv=new ModelAndView("redirect:selectAll.emp");
      return mv;
    }

    //跳到修改权限页面
    @RequestMapping("toEditPowerPage.emp")
    public  ModelAndView toEditPowerPage(Long powerId){
        ModelAndView mv=new ModelAndView("backEnd/powerEdit");
        //selectByPrimaryKey
        Xzpower xzpower =powerService.selectByPrimaryKey(powerId);
        xzpower.setRealPowerId(powerId);
        //查询所有的父级权限
        List<Xzpower> superPowerList=powerService.selsuperNoAll();
        mv.addObject("superPowerList",superPowerList);
        mv.addObject("xzpower",xzpower);
        mv.addObject("msg","update");
        if(!xzpower.getSuperNo().equals("0")) {
            mv.addObject("show", "show");
        }
        return mv;
    }

    //修改权限的方法
    @RequestMapping("updatePower.emp")
    public ModelAndView updatePower(Xzpower xzpower,Long realPowerId){
        ModelAndView mv=new ModelAndView("redirect:selectAll.emp");
//        Xzpower newXzpower=new Xzpower();
//        newXzpower.setSuperNo(Integer.parseInt(String.valueOf(xzpower.getPowerId())));
        //修改是先删除该条数据再添加
        //*******************删除******************************************************
        Xzpower deleteXzpower=new Xzpower();
        Date newtime = new Date();
        deleteXzpower.setDeleteTime(newtime);
        deleteXzpower.setPowerId(realPowerId);
        powerService.phoneyDeleteByPrimaryKey(deleteXzpower);
        //*******************添加*****************************************************
        //查询出原始的对象
        Xzpower  OldXzpower=powerService.selectByPrimaryKey(realPowerId);

        //添加一个权限先添加它的模板级权限
        Xzpower PowerTypeOfXzpower =powerService.selectByPrimaryKey(xzpower.getPowerId());
        if(PowerTypeOfXzpower == null){

            if(powerService.selAll().size()==0){
                xzpower.setPowerType(1);
            }else {
                int PowerType= powerService.selePowerTypeMaxOne()+1;
                xzpower.setPowerType(PowerType);
            }
        }else {
            xzpower.setPowerType(Integer.parseInt(PowerTypeOfXzpower.getPowerNo()));
        }

        //当无父级权限号时
        if(xzpower.getPowerId() == 0) {
            //数据库中的表没任何数据
            List<Xzpower> superPowerList = powerService.selsuperNoAll();
            if (superPowerList.size() == 0) {
                xzpower.setPowerNo("1");   //添加本级的权限序号为0
                xzpower.setSuperNo("0");   //添加本级的权限序号为1
            } else {
//                xzpower.setPowerNo(OldXzpower.getPowerNo());
                String newPowerNo = powerService.selePowerNoMaxOne("0");
                int newPowerNoInt=Integer.parseInt(newPowerNo)+1;  //新的本级权限号需加1
                newPowerNo=String.valueOf(newPowerNoInt);
                xzpower.setPowerNo(newPowerNo);
                xzpower.setSuperNo("0");
            }
        } else {   //当有父级权限时
            //根据主键查询
            if(OldXzpower.getSuperNo().equals(xzpower.getSuperNo())) {
                xzpower.setPowerNo(OldXzpower.getPowerNo());
                xzpower.setSuperNo(OldXzpower.getSuperNo());
            }else {
                if (xzpower.getSuperNo() == null || xzpower.getSuperNo().equals("0")) {    //（有模板级没有权限级时）
                    Xzpower newXzpower = powerService.selectByPrimaryKey(xzpower.getPowerId());
                    xzpower.setSuperNo(newXzpower.getPowerNo());
                    //当此父级权限下的子级权限第一个添加时
                    if (powerService.selePowerNoMaxOne(xzpower.getSuperNo()) == null) {
                        xzpower.setPowerNo(String.valueOf(xzpower.getSuperNo()) + "_1");
                    } else {
                        String newPowerNo = powerService.selePowerNoMaxOne(xzpower.getSuperNo());
                        //截取原先的最大的本级权限号后再加上1，得到新的本级权限再加里去
                        String[] units = newPowerNo.split("_");
                        newPowerNo = String.valueOf(Integer.parseInt(units[units.length - 1]) + 1);
                        String superNo = units[0].toString();
                        newPowerNo = superNo + "_" + newPowerNo;   //新的本级权限号
                        xzpower.setPowerNo(newPowerNo);
                    }

                } else {
                    String newPowerNo = powerService.selePowerNoMaxOne(xzpower.getSuperNo());
                    if(newPowerNo == null){  //该权限级下第一次添加
                        newPowerNo=xzpower.getSuperNo()+"_1";
                    }else {   //该权限级下不是第一次添加
                        String[] units = newPowerNo.split("_");
                        newPowerNo = String.valueOf(Integer.parseInt(units[units.length - 1]) + 1);
                        newPowerNo=xzpower.getSuperNo()+"_"+newPowerNo;
                    }
                    xzpower.setPowerNo(newPowerNo);
                }
            }

        }
        //添加权限
        int i= powerService.insertPower(xzpower);
        return mv;
    }

    //伪删除
    @RequestMapping("deletePower.emp")
    public ModelAndView  deletePower(Long powerId){
       ModelAndView mv=new ModelAndView("redirect:selectAll.emp");
        Xzpower xzpower=new Xzpower();
        Date newtime = new Date();
        xzpower.setPowerId(powerId);
        xzpower.setDeleteTime(newtime);
       powerService.phoneyDeleteByPrimaryKey(xzpower);
       return mv;
    }

    //根据模块查询权限
    @ResponseBody
    @RequestMapping("selPowerLevel.emp")
    public Map<String,Object> selPowerLevel(Long id){
         Map<String,Object> map=new HashMap<>();
        Xzpower xzpower=powerService.selectByPrimaryKey(id); //根据主键查询
         List<Xzpower> PowerLevelList=powerService.selePowerLevel(xzpower.getPowerNo());
         for(int i=0;i<PowerLevelList.size();i++){
             PowerLevelList.get(i).setPowerLevelId(PowerLevelList.get(i).getPowerNo());
         }
         map.put("PowerLevelList",PowerLevelList);
         return map;
    }

}
